package cn.powerone.autotest.controller;

import cn.powerone.autotest.domain.Project;
import cn.powerone.autotest.service.IProjectService;
import cn.powerone.common.annotation.Log;
import cn.powerone.common.constant.UserConstants;
import cn.powerone.common.core.controller.BaseController;
import cn.powerone.common.core.domain.AjaxResult;
import cn.powerone.common.core.page.TableDataInfo;
import cn.powerone.common.enums.BusinessType;
import cn.powerone.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * 自动化测试项目管理Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/autotest/projects")
public class ProjectController extends BaseController
{
    @Autowired
    private IProjectService projectService;

    /**
     * 查询项目列表
     */
    @PreAuthorize("@ss.hasPermi('autotest:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(Project project)
    {
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 导出项目列表
     */
    @PreAuthorize("@ss.hasPermi('autotest:project:export')")
    @GetMapping("/export")
    public AjaxResult export(Project project)
    {
        List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        return util.exportExcel(list, "项目数据");
    }

    /**
     * 获取项目详细信息
     * 
     * @param projectId 项目ID
     * @return 项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('autotest:project:query')")
    @GetMapping(value = "{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") String projectId)
    {
        return AjaxResult.success(projectService.selectProjectById(projectId));
    }

    /**
     * 新增项目
     * 
     * @param project 项目信息
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('autotest:project:add')")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Project project)
    {
        if (projectService.checkProjectNameUnique(project))
        {
            return AjaxResult.error("新增项目'" + project.getProjectName() + "'失败，项目名称已存在");
        }
        // 生成UUID作为项目ID
        project.setProjectId(UUID.randomUUID().toString());
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改项目
     */
    @PreAuthorize("@ss.hasPermi('autotest:project:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Project project)
    {
        project.setUpdateBy(getUsername());
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除项目
     * 
     * @param projectIds 需要删除的项目ID
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('autotest:project:remove')")
    @Log(title = "项目", businessType = BusinessType.DELETE)
    @DeleteMapping("{projectIds}")
    public AjaxResult remove(@PathVariable String[] projectIds)
    {
        return toAjax(projectService.deleteProjectByIds(projectIds));
    }
}